const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchList: [],
    hotList: [],
    clickSearch: false,
    key: "",
    sortType: 1,
    page: 1,
    activityList: [],
    loadingStatus: false,
    msgStatus: false
  },
  // 排序
  sortList(e) {
    let that = this;
    let sortType = e.target.dataset.type;
    let thatSortType = that.data.sortType;
    that.setData({
      msgStatus: false
    })
    if (sortType == 1 || sortType == 2) {
      if (sortType == thatSortType) {
        wx.hideLoading();
        return;
      } else {
        that.setData({
          sortType: sortType
        })
      }
    } else if (sortType == 3) {
      if (thatSortType == 3) {
        that.setData({
          sortType: 4
        })
      } else {
        that.setData({
          sortType: 3
        })
      }
    }
    wx.showLoading({
      title: '排序中...',
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data: {
        page: 1,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            activityList: res.data.data,
            page: 1
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 加载更多
  load() {
    let that = this;
    if (that.data.loadingStatus) { return; }
    if (that.data.msgStatus) { return; }
    that.setData({
      loadingStatus: true
    })
    let page = that.data.page;
    page = ++page;
    wx.showLoading({
      title: '正在加载更多',
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data: {
        page: page,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              msgStatus: true
            })
          } else {
            that.setData({
              activityList: that.data.activityList.concat(res.data.data),
              msgStatus: false
            })
          }
        } else {
          page = --page;
          app.showToast(res.data.msg)
        }
        that.setData({
          page: page,
          loadingStatus: false
        })
      }
    })
  },
  //获取搜索值
  searchIpt(e) {
    let that = this;
    that.setData({
      key: e.detail.value
    })
    if (e.detail.value == "") {
      that.setData({
        clickSearch: false
      })
      //获取最近搜索
      wx.request({
        url: app.buildUrl('/search/get-user-words'),
        header: app.buildHeader(),
        success(res) {
          if (res.data.code == 200) {
            that.setData({
              searchList: res.data.data
            })
          }
        }
      })
    }
  },
  //搜索
  search() {
    let that = this;
    if (that.data.key == "") {
      app.showToast('请输入搜索关键字')
    } else {
      wx.showLoading({
        title: '搜索中...',
      })
      that.setData({
        page: 1,
        clickSearch: true,
        sortType: 1
      })
      //将搜索字添加
      wx.request({
        url: app.buildUrl('/search/add-hot-word'),
        header: app.buildHeader(),
        data: {
          search_type: 1,
          search_word: that.data.key
        },
        success(res) { }
      })
      wx.request({
        url: app.buildUrl('/search/get-lists'),
        header: app.buildHeader(),
        data: {
          page: that.data.page,
          search_word: that.data.key,
          type: that.data.sortType
        },
        success(res) {
          wx.hideLoading()
          if (res.data.code == 200) {
            that.setData({
              activityList: res.data.data
            })
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    }
  },
  // 点击最近搜索、热门搜索
  wordSearch(e) {
    let that = this;
    that.setData({
      key: e.target.dataset.key,
      clickSearch: true,
      sortType: 1,
      page: 1
    })
    wx.showLoading({
      title: '搜索中...',
    })
    that.setData({
      page: 1,
      clickSearch: true
    })
    //将搜索字添加
    wx.request({
      url: app.buildUrl('/search/add-hot-word'),
      header: app.buildHeader(),
      data: {
        search_type: e.target.dataset.type,
        search_word: that.data.key
      },
      success(res) { }
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data: {
        page: that.data.page,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          that.setData({
            activityList: res.data.data
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  //删除最近搜索
  del() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要清除最近搜索记录？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.buildUrl('/search/del-user-words'),
            header: app.buildHeader(),
            success(res) {
              if (res.data.code == 200) {
                that.setData({
                  searchList: []
                })
              }
              app.showToast(res.data.msg)
            }
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //获取最近搜索
    wx.request({
      url: app.buildUrl('/search/get-user-words'),
      header: app.buildHeader(),
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            searchList: res.data.data
          })
        }
      }
    })

    // 获取热门搜索
    wx.request({
      url: app.buildUrl('/search/get-hot-words'),
      header: app.buildHeader(),
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            hotList: res.data.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})